package com.kk.emp.controller;

import com.github.pagehelper.PageInfo;
import com.kk.emp.entity.Emp;
import com.kk.emp.service.EmpService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/*
 *@author KK on 2022.10.08 0:50
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private EmpService empService;

    @GetMapping("/getEmpByPage")
    public String getEmpByPage(@RequestParam Map<String, String> emps, Model model) {
        PageInfo<Emp> info = empService.getEmpByPage(emps);
        model.addAttribute("info", info);
        return "index";
    }

    @RequestMapping("/addEmp")
    public String addEmp(Emp emp,Model model){
        if (empService.addEmp(emp)) {
            model.addAttribute("msg","添加成功");
        }
        else {
            model.addAttribute("msg","添加失败");
        }
        return "message";
    }

    @RequestMapping("/delEmp")
    @ResponseBody
    public String delEmp(Integer eno){
        if (empService.delEmp(eno)) {
            return "1";
        }
        return "0";
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public String checkEmpName(String ename) {
        if (empService.checkName(ename)) {
            // 返回音响 用户名不可用
            return "0";
        }
        // 返回响应 用户名可用
        return "1";
    }
}
